﻿package stfei
{
	/**
	 * 加载影片进度
	 */
	import flash.display.*;
	import flash.events.*;
	import flash.text.TextField;
	public class Preloader extends Sprite
	{
		public var loadMC:Loader
		private var txt:TextField;		
		private var setMC:MovieClip;
		public static const LOADOVER:String = "loadover";
		public function Preloader(loader:Loader, myMc:MovieClip)
		{
			setMC = myMc;
			loadMC = loader
			this.configEve(loadMC.contentLoaderInfo);
			
		}
		private function configEve(dispatcher:IEventDispatcher):void
		{
			dispatcher.addEventListener(Event.OPEN, openHandler);
			dispatcher.addEventListener(ProgressEvent.PROGRESS, progress);
			dispatcher.addEventListener(Event.COMPLETE, complete);
		}
		//开始加载事件
		private function openHandler(eve:Event):void
		{
			txt = new TextField();
			txt.x = setMC.width / 2 - 10
			txt.y = setMC.height /2 -10
			txt.selectable = false;
		}
		//更新加载进度事件
		private function progress(eve:ProgressEvent):void
		{
			var mubytes:Number = eve.bytesLoaded / eve.bytesTotal;
			var muMatby:Number = Math.round(100 * mubytes);

			txt.text = String(muMatby);
			setMC.addChild(txt);
		}
		//完成加载事件
		private function complete(eve:Event):void
		{
			//创建完成事件监听器
			this.dispatchEvent(new Event(Preloader.LOADOVER,false,false));
			eve.currentTarget.removeEventListener(ProgressEvent.PROGRESS, progress);
			setMC.removeChild(txt);
		}
	}
}